package com.anos.smsfun.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.anos.smsfun.R;
import com.anos.smsfun.model.ContactsWrapper;
import com.anos.smsfun.utility.DefConstants;
import com.anos.smsfun.utility.Utils;

public class ContactDetailActivity extends Activity implements OnClickListener {

	private TextView tvDisplayName;
	private TextView tvDisplayNumber;
	private Button btnCall;
	private Button btnSms;

	private ContactsWrapper mContactsWrapper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contact_detail);

		Utils.setUpActionBar(this);

		// get data
		try {
			mContactsWrapper = (ContactsWrapper) getIntent().getExtras().get(
					DefConstants.ACTION_VIEW_DETAIL_CONTACT);
			if (mContactsWrapper == null)
				finish();
		} catch (Exception e) {
			e.printStackTrace();
			finish();
		}

		// find view id all
		findViewByIdAll();

		// set data
		setDataForView();
	}

	private void findViewByIdAll() {
		tvDisplayName = (TextView) findViewById(R.id.tvDisplayNameDetail);
		tvDisplayNumber = (TextView) findViewById(R.id.tvPhoneNumberDetail);
		btnCall = (Button) findViewById(R.id.btnCallDetail);
		btnSms = (Button) findViewById(R.id.btnSMSDetail);

		btnCall.setOnClickListener(this);
		btnSms.setOnClickListener(this);
	}

	private void setDataForView() {
		// set title view
		LayoutInflater inflator = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflator.inflate(R.layout.title_view, null);
		((TextView) v.findViewById(R.id.tvTitle_ab)).setText(mContactsWrapper
				.getDisplayName());
		((TextView) v.findViewById(R.id.tvNumber_ab)).setText(mContactsWrapper
				.getDisplayNumber());
		getActionBar().setCustomView(v);

		tvDisplayName.setText(mContactsWrapper.getDisplayName() + "");
		tvDisplayNumber.setText(mContactsWrapper.getDisplayNumber() + "");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_detail, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			overridePendingTransition(0, R.anim.exit_slide_right);
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnCallDetail:
			if (!mContactsWrapper.getDisplayNumber().trim().equals("")) {
				Utils.callPhone(ContactDetailActivity.this, mContactsWrapper
						.getDisplayNumber().trim());
			} else {
				Utils.showMessageDialog(ContactDetailActivity.this,
						getString(R.string.can_not_call));
			}
			break;

		case R.id.btnSMSDetail:
			Intent intent = new Intent(ContactDetailActivity.this,
					ViewSmsConversationActivity.class);
			intent.putExtra(DefConstants.ACTION_VIEW_CONVERSATION,
					mContactsWrapper.getDisplayNumber().trim());
			ContactDetailActivity.this.startActivity(intent);
			ContactDetailActivity.this.overridePendingTransition(0,
					R.anim.exit_slide_left);
			break;

		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		overridePendingTransition(0, R.anim.exit_slide_right);
		return;
	}
}
